package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDy;
import cn.stylefeng.guns.sys.modular.system.entity.BtyElm;
import cn.stylefeng.guns.sys.modular.system.entity.BtyElmsxf;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyDyMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDyParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyElmsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyDyResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyElmsxfResult;
import  cn.stylefeng.guns.sys.modular.system.service.BtyDyService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuy
 * @since 2022-01-24
 */
@Service
public class BtyDyServiceImpl extends ServiceImpl<BtyDyMapper, BtyDy> implements BtyDyService {

    @Override
    public void add(BtyDyParam param){
        BtyDy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyDyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyDyParam param){
        BtyDy oldEntity = getOldEntity(param);
        BtyDy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyDyResult findBySpec(BtyDyParam param){
        return null;
    }

    @Override
    public List<BtyDyResult> findListBySpec(BtyDyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyDyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 导入excel
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean batchImport(String fileName, MultipartFile file,BtyDyParam param){
        boolean notNull = false;
        try {
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                throw new Exception("上传文件格式不正确");
            }
            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            Workbook wb = null;
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
            //导入之前删掉之前的数据
            baseMapper.delete(new QueryWrapper<>());
            //excel中多个sheet表，获取所有sheet表的数量

            //System.out.println("sheets:"+sheets);

            int sheets = wb.getNumberOfSheets();
            for (int i = 0; i < sheets; i++) {
                Sheet sheet = wb.getSheetAt(i);
                //Sheet sheet = wb.getSheetAt(0);
                if (sheet != null) {
                    notNull = true;
                }

                int lastRowNum = sheet.getLastRowNum();
                for (int r = 1; r <= lastRowNum; r++) {
                    Row row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
                    //获取单元格
                    Cell cell = row.getCell(1);
                    if (ToolUtil.isNotEmpty(cell)) {
                        cell.setCellType(CellType.NUMERIC);
                    }

                    Cell cell2 = row.getCell(2);
                    if (ToolUtil.isNotEmpty(cell2)) {
                        cell2.setCellType(CellType.STRING);
                    }
                    Cell cell3 = row.getCell(3);
                    if (ToolUtil.isNotEmpty(cell3)) {
                        cell3.setCellType(CellType.STRING);
                    }

                    //店名
                    String dm = row.getCell(0) == null ? "" : row.getCell(0).getStringCellValue();
                    //结算金额
                    Double jsje = row.getCell(1) == null ? 0 : row.getCell(1).getNumericCellValue();
                    //大区
                    String region=row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue();
                    //状态
                    String state=row.getCell(3) == null ? "0" : row.getCell(3).getStringCellValue();
                    BtyDy dy=new BtyDy();
                    //dy.setId(UUIDUtils.getId());
                    dy.setDm(dm);
                    dy.setAmount(new BigDecimal(jsje).setScale(2, BigDecimal.ROUND_HALF_UP));
                    dy.setBh(region);
                    dy.setState(Integer.parseInt(state));
                    baseMapper.insert(dy);
                }
            }

            //导入成功之后按状态和大区生成凭证
            String zdrq=param.getZdrq();
            //全国
            List<BtyDyResult>list=baseMapper.dyList(1);

            List<BtyDyResult>listQg=baseMapper.dyList(2);
            //江苏
            List<BtyDyResult>listJs=baseMapper.dyList(3);
            //安徽
            List<BtyDyResult>listAh=baseMapper.dyList(4);
            if(list.size()>0){
                for (BtyDyResult dy : list) {
                    BigDecimal amount=dy.getAmount();
                    String bh=dy.getBh();
                    String yaoma=dy.getYaoma();
                    String kh=dy.getKh();
                    String dm=dy.getDm();
                    RestTemplate restTemplate = new RestTemplate();
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                    headers.add("system", "001"); // 系统编码
                    headers.add("usercode", "15253172037"); // 用户
                    headers.add("password", "514576f58a89767b46c1b44767a08f77");
                    headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                    StringBuilder sb=new StringBuilder();
                    sb.append("{\n" +
                            "    \"voucher\":[\n" +
                            "        {\n" +
                            "            \"details\":[");

                    //全国抖音凭证
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"96\",\n" +
                            "                            \"checkvaluecode\":'"+kh+"'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+amount+"\",\n" +
                            "                    \"explanation\":\"全国抖音\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"100202\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," +
                            "{\n" +
                            "\t\t\t\t\"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "\t\t\t\t\t\t{\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":\"0323\"\n" +//全国抖音辅助核算编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\""+amount+"\",\n" +
                            "                    \"creditquantity\":\"\",\n" +
                            "                    \"debitamount\":\"\",\n" +
                            "                    \"debitquantity\":\"\",\n" +
                            "                    \"detailindex\":\"\",\n" +
                            "                    \"excrate1\":\"\",\n" +
                            "                    \"explanation\":\"全国抖音\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112231\",\n" +
                            "                    \"pk_currtype\":\"CNY\",\n" +
                            "                    \"price\":\"\"\n" +
                            "                },\n");
                    sb.append("            ],\n" +
                            "            \"pk_corp\":"+bh+",\n" +
                            "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                            "            \"pk_prepared\":\"15253172037\",\n" +
                            "            \"pk_vouchertype\":\"记账\",\n" +
                            "            \"prepareddate\":\""+zdrq+"\"\n" +
                            "        }\n" +
                            "    ]\n" +
                            "}");
                    //System.out.println("sb:"+sb.toString());
                    HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                    String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                    String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                    //System.out.println("result:"+result);
                    JsonParser jp = new JsonParser();
                    //将json字符串转化成json对象
                    JsonObject jo = jp.parse(result).getAsJsonObject();
                    //获取errcode对应的值
                    String status = jo.get("status").getAsString();

                if(status.equals("falied")){
                    String errormsg = jo.get("errormsg").getAsString();
                    BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",1));
                    if(ToolUtil.isNotEmpty(dy1)){
                        dy1.setErrormsg(errormsg);
                        baseMapper.updateById(dy1);
                    }
                    //System.out.println("errormsg:"+errormsg);
                }else{
                    BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",1));
                    if(ToolUtil.isNotEmpty(dy1)){
                        dy1.setFlag(1);
                        baseMapper.updateById(dy1);
                    }
                    //System.out.println("凭证导入成功！");
                 }
                }
            }
            if(listQg.size()>0){
                for (BtyDyResult dy : listQg) {
                    BigDecimal amount=dy.getAmount();
                    String bh=dy.getBh();
                    String yaoma=dy.getYaoma();
                    String kh=dy.getKh();
                    String dm=dy.getDm();
                    RestTemplate restTemplate = new RestTemplate();
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                    headers.add("system", "001"); // 系统编码
                    headers.add("usercode", "15253172037"); // 用户
                    headers.add("password", "514576f58a89767b46c1b44767a08f77");
                    headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                    StringBuilder sb=new StringBuilder();
                    sb.append("{\n" +
                            "    \"voucher\":[\n" +
                            "        {\n" +
                            "            \"details\":[");

                    //全国抖音凭证
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"96\",\n" +
                            "                            \"checkvaluecode\":'"+kh+"'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+amount+"\",\n" +
                            "                    \"explanation\":\"全国抖音\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"100202\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," +
                            "{\n" +
                            "\t\t\t\t\"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "\t\t\t\t\t\t{\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":\"0323\"\n" +//全国抖音辅助核算编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\""+amount+"\",\n" +
                            "                    \"creditquantity\":\"\",\n" +
                            "                    \"debitamount\":\"\",\n" +
                            "                    \"debitquantity\":\"\",\n" +
                            "                    \"detailindex\":\"\",\n" +
                            "                    \"excrate1\":\"\",\n" +
                            "                    \"explanation\":\"全国抖音\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112231\",\n" +
                            "                    \"pk_currtype\":\"CNY\",\n" +
                            "                    \"price\":\"\"\n" +
                            "                },\n");
                    sb.append("            ],\n" +
                            "            \"pk_corp\":"+bh+",\n" +
                            "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                            "            \"pk_prepared\":\"15253172037\",\n" +
                            "            \"pk_vouchertype\":\"记账\",\n" +
                            "            \"prepareddate\":\""+zdrq+"\"\n" +
                            "        }\n" +
                            "    ]\n" +
                            "}");
                    //System.out.println("sb:"+sb.toString());
                    HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                    String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                    String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                    //System.out.println("result:"+result);
                    JsonParser jp = new JsonParser();
                    //将json字符串转化成json对象
                    JsonObject jo = jp.parse(result).getAsJsonObject();
                    //获取errcode对应的值
                    String status = jo.get("status").getAsString();
                    if(status.equals("falied")){
                        String errormsg = jo.get("errormsg").getAsString();
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",2));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setErrormsg(errormsg);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("errormsg:"+errormsg);
                    }else{
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",2));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setFlag(1);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("凭证导入成功！");
                    }
                }
            }
            if(listJs.size()>0){
                for (BtyDyResult dy : listJs) {
                    BigDecimal amount=dy.getAmount();
                    String bh=dy.getBh();
                    String yaoma=dy.getYaoma();
                    String kh=dy.getKh();
                    String dm=dy.getDm();
                    RestTemplate restTemplate = new RestTemplate();
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                    headers.add("system", "001"); // 系统编码
                    headers.add("usercode", "15253172037"); // 用户
                    headers.add("password", "514576f58a89767b46c1b44767a08f77");
                    headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                    StringBuilder sb=new StringBuilder();
                    sb.append("{\n" +
                            "    \"voucher\":[\n" +
                            "        {\n" +
                            "            \"details\":[");

                    //江苏凭证
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"96\",\n" +
                            "                            \"checkvaluecode\":'"+kh+"'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+amount+"\",\n" +
                            "                    \"explanation\":\"抖音收款\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"100202\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," +
                            "{\n" +
                            "\t\t\t\t\"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "\t\t\t\t\t\t{\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":\"0017\"\n" +//抖音收款辅助核算编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\""+amount+"\",\n" +
                            "                    \"creditquantity\":\"\",\n" +
                            "                    \"debitamount\":\"\",\n" +
                            "                    \"debitquantity\":\"\",\n" +
                            "                    \"detailindex\":\"\",\n" +
                            "                    \"excrate1\":\"\",\n" +
                            "                    \"explanation\":\"抖音收款\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112220\",\n" +
                            "                    \"pk_currtype\":\"CNY\",\n" +
                            "                    \"price\":\"\"\n" +
                            "                },\n");
                    sb.append("            ],\n" +
                            "            \"pk_corp\":"+bh+",\n" +
                            "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                            "            \"pk_prepared\":\"15253172037\",\n" +
                            "            \"pk_vouchertype\":\"记账\",\n" +
                            "            \"prepareddate\":\""+zdrq+"\"\n" +
                            "        }\n" +
                            "    ]\n" +
                            "}");
                    //System.out.println("sb:"+sb.toString());
                    HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                    String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                    String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                    //System.out.println("result:"+result);
                    JsonParser jp = new JsonParser();
                    //将json字符串转化成json对象
                    JsonObject jo = jp.parse(result).getAsJsonObject();
                    //获取errcode对应的值
                    String status = jo.get("status").getAsString();
                    if(status.equals("falied")){
                        String errormsg = jo.get("errormsg").getAsString();
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",3));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setErrormsg(errormsg);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("errormsg:"+errormsg);
                    }else{
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",3));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setFlag(1);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("凭证导入成功！");
                    }

                }
            }
            if(listAh.size()>0){
                for (BtyDyResult dy : listAh) {
                    BigDecimal amount=dy.getAmount();
                    String bh=dy.getBh();
                    String yaoma=dy.getYaoma();
                    String kh=dy.getKh();
                    String dm=dy.getDm();
                    RestTemplate restTemplate = new RestTemplate();
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                    headers.add("system", "001"); // 系统编码
                    headers.add("usercode", "15253172037"); // 用户
                    headers.add("password", "514576f58a89767b46c1b44767a08f77");
                    headers.add("Accept", MediaType.APPLICATION_JSON.toString());

                    StringBuilder sb=new StringBuilder();
                    sb.append("{\n" +
                            "    \"voucher\":[\n" +
                            "        {\n" +
                            "            \"details\":[");

                    //安徽直播凭证
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"96\",\n" +
                            "                            \"checkvaluecode\":'"+kh+"'\n" +
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+amount+"\",\n" +
                            "                    \"explanation\":\"安徽直播\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"100202\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                }," +
                            "{\n" +
                            "\t\t\t\t\"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'"+yaoma+"'\n" +
                            "                        },\n" +
                            "\t\t\t\t\t\t{\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":\"0319\"\n" +//直播收款辅助核算编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\""+amount+"\",\n" +
                            "                    \"creditquantity\":\"\",\n" +
                            "                    \"debitamount\":\"\",\n" +
                            "                    \"debitquantity\":\"\",\n" +
                            "                    \"detailindex\":\"\",\n" +
                            "                    \"excrate1\":\"\",\n" +
                            "                    \"explanation\":\"安徽直播\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112220\",\n" +
                            "                    \"pk_currtype\":\"CNY\",\n" +
                            "                    \"price\":\"\"\n" +
                            "                },\n");
                    sb.append("            ],\n" +
                            "            \"pk_corp\":"+bh+",\n" +
                            "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                            "            \"pk_prepared\":\"15253172037\",\n" +
                            "            \"pk_vouchertype\":\"记账\",\n" +
                            "            \"prepareddate\":\""+zdrq+"\"\n" +
                            "        }\n" +
                            "    ]\n" +
                            "}");
                    //System.out.println("sb:"+sb.toString());
                    HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                    String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                    String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                    //System.out.println("result:"+result);
                    JsonParser jp = new JsonParser();
                    //将json字符串转化成json对象
                    JsonObject jo = jp.parse(result).getAsJsonObject();
                    //获取errcode对应的值
                    String status = jo.get("status").getAsString();
                    if(status.equals("falied")){
                        String errormsg = jo.get("errormsg").getAsString();
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",4));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setErrormsg(errormsg);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("errormsg:"+errormsg);
                    }else{
                        BtyDy dy1=baseMapper.selectOne(new QueryWrapper<BtyDy>().eq("dm",dm).eq("state",4));
                        if(ToolUtil.isNotEmpty(dy1)){
                            dy1.setFlag(1);
                            baseMapper.updateById(dy1);
                        }
                        //System.out.println("凭证导入成功！");
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return notNull;
    }

    @Override
    public List<BtyDyResult> customList(BtyDyParam param) {
        return baseMapper.customList(param);
    }

    private Serializable getKey(BtyDyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyDy getOldEntity(BtyDyParam param) {
        return this.getById(getKey(param));
    }

    private BtyDy getEntity(BtyDyParam param) {
        BtyDy entity = new BtyDy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
